// StreamDriverDisable.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <devload.h>

int LoadDisableInRegistry();


int LoadRegistry();
int LoadDriver();
TCHAR szDevKey[] = _T("Drivers\\BuiltIn\\UART1");
TCHAR szCOMPort[] = L"COM1:";

int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR     lpCmdLine,
	int       nCmdShow)
{
	LPCTSTR   wstream;
	BOOL usedCommandLine = true;

	wprintf(L"StreamDriverDisable App.\r\nModifies registry so driver won't load, esp. at boot\r\n");

	wstream = (LPCTSTR)lpCmdLine;

	DWORD cmdlineLength = wcslen(wstream);

	//Default to this stream
	if (cmdlineLength == 0)
	{
		usedCommandLine = false;
	}
	//Check for index no.
	else if (cmdlineLength == 1)
	{
		BYTE index = (BYTE)lpCmdLine[0] - (BYTE) '0';
		if (index < 7)
		{
			//Replace 1 in UART1 with index
			szDevKey[wcslen(szDevKey) - 1] = (WCHAR)lpCmdLine[0];
			szCOMPort[wcslen(szCOMPort) - 2] = (WCHAR)lpCmdLine[0];
			usedCommandLine = true;
		}
		//Otherwise default to this stream with index 1
		else
		{
			usedCommandLine = false;
		}
	}
	//Otherwise default to this stream with index 1
	else if (cmdlineLength != 5)
	{
		usedCommandLine = false;
	}

	wprintf(L"Working with registry key: %s\r\n", szDevKey);
	int res = LoadDisableInRegistry();
	if (res != 0) return res;
	return 0;
}

int LoadDisableInRegistry()
{

	//TCHAR szDevKey[]  = _T("Drivers\\UART"); 
	//TCHAR szDllName[] = _T("am33x_uart.dll");
	//TCHAR szPrefix[]  = _T("COM");
	HKEY   hKey;
	DWORD  dwDisposition;
	DWORD dwData = 4; // NO_LOAD ;  //Load the driver in user mode. was 4: Don't load

	DWORD result = 0;



	// Open the registry key and add these values
	LONG res1 = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
		szDevKey,
		0,
		_T("StreamDriver"),
		REG_OPTION_VOLATILE,
		0,
		NULL,
		&hKey,
		&dwDisposition);

	if (res1 == ERROR_SUCCESS) {
		if (dwDisposition == REG_OPENED_EXISTING_KEY)
			wprintf(L"Registry key openned OK.\r\n");
		else
		{
			wprintf(L"Registry key created new, didn't exist, exiting.\r\n");
			return -1;
		}

	}
	else {
		wprintf(L"Registry key open failed.\r\n");
		return -1;
	}


	// Modify the key specifying the UserMode Load
	LONG res4 = RegSetValueEx( hKey,	//subkey handle
	_T("Flags"),			// value name
	0,							// must be zero
	REG_DWORD,					// value type
	(LPBYTE) &dwData,			// pointer to value data
	sizeof(DWORD));				// length of value data */

	/*LONG res4 = RegDeleteValue(hKey,	//subkey handle 
		_T("Flags"));			// value name */


	if (res4 == ERROR_SUCCESS) {
		wprintf(L"Registry Flags set OK.\r\n", szDevKey);

	}
	else {
		wprintf(L"Registry Flags set failed.\r\n", szDevKey);

		return -4;
	}

	RegFlushKey(hKey);

	LONG res5 = RegCloseKey(hKey);
	if (res5 == ERROR_SUCCESS) {
		wprintf(L"Registry key closed OK.\r\n");
	}
	else {
		wprintf(L"Registry key closure failed.\r\n");
		return -5;
	}

	return 0;
}